/**
*  \file parameter.c
*
*  \brief
*
*  \author Michael J. Hohmann  <michael.hohmann@linde-mh.de>
*
*
*/

/*===============================================[ private includes  ]================================================*/
#include "parameter.h"
#include "../assertor/assertor.h"
#include "../paragen/paragen.h"
#include "../pal/pal_mem.h"
#include "../nvdata/nvdata.h"
#include "../nvram/nvram.h"


/*===============================================[ private defines   ]================================================*/

/*===============================================[ private datatypes ]================================================*/

/** Private variables */
struct parameter_private
{
   U8 dummy; /**<  */
};

/*===============================================[ private variables ]================================================*/
static struct parameter_private parameter;
struct EDA_sttyIdentifier       stEcuIdentifier;
nv_readWriteparam        		stReadWriteParam;

static RC parameter_set(  U32 index, void const * data, U16 size, U32  writelevel );
static RC parameter_get( U32 index, void  *data, U32 *size , U32 readlevel );

/*===============================================[ private functions ]================================================*/

/*================================================[ inline functions ]================================================*/

/*===============================================[ public variables ]================================================*/
nv_readWriteparam*  const pReadWriteparam  = (nv_readWriteparam*)(ADDRESS_READWRITE_PARAM);

/*================================================[ public functions ]================================================*/
RC parameter_initialize( void )
{
   pal_memzero( &parameter, sizeof parameter);


   paragen_load( &calibration_obj );
   paragen_load( &service_obj );
   paragen_load( &production_obj );
   sd_read( NVRAM_SD, &stEcuIdentifier,      (sd_address_t) ((U32)&nvdata->stIdentifier), 0x100);
   sd_read( NVRAM_SD, &stReadWriteParam,(sd_address_t)((U32)ADDRESS_READWRITE_PARAM), SIZE_READWRITE_PARAM);
   dtc_init();
   return RC_SUCCESS;
}

enum kwp_response_code parameter_read_kwp( U32 index, void  *data, U16 *size , U32 readlevel )
{
   U32 data_len = 0;

   switch(  parameter_get(index, data, &data_len, readlevel ) )
   {
      case RC_ERROR_OPEN:                  return KWP_RESPONSE_CODE_INVALIDFORMAT;
      case RC_ERROR_READ_FAILS:            return KWP_RESPONSE_CODE_INVALIDFORMAT;
      case RC_ERROR_INVALID:               return KWP_RESPONSE_CODE_INVALIDFORMAT;
      case RC_SUCCESS: *size +=  data_len ;return KWP_RESPONSE_CODE_SUCCESS;
      default:                             return KWP_RESPONSE_CODE_SUCCESS;
   }
}

enum kwp_response_code parameter_write_kwp( U32 index, void  *data, U16 size , U32 readlevel )
{
   switch(  parameter_set(index, data, (U32)size, readlevel ) )
   {
      case RC_ERROR_OPEN:                 return KWP_RESPONSE_CODE_INVALIDFORMAT;
      case RC_ERROR_READ_FAILS:           return KWP_RESPONSE_CODE_INVALIDFORMAT;
      case RC_ERROR_INVALID:              return KWP_RESPONSE_CODE_INVALIDFORMAT;
      case RC_ERROR_RANGE:                return KWP_RESPONSE_CODE_REQUESTOUTOFRANGE;
      case RC_ERROR_FILE_ACCESS:          return KWP_RESPONSE_CODE_ACCESSDENIED;
      case RC_ERROR:                      return KWP_RESPONSE_CODE_GENERALREJECT;
      case RC_SUCCESS:                    return KWP_RESPONSE_CODE_SUCCESS;
      default:                            return KWP_RESPONSE_CODE_SUCCESS;
   }
}

static RC parameter_get( U32 index, void  *data, U32 *size , U32 readlevel )
{   RC rv=RC_ERROR_OPEN;

    rv = paragen_get( &calibration_obj, index,data,size,readlevel);
    if (rv!=RC_ERROR_OPEN)
    {
        return rv;
    }

    rv = paragen_get( &service_obj, index,data,size,readlevel);
    if (rv!=RC_ERROR_OPEN)
    {
        return rv;
    }

    rv = paragen_get( &production_obj, index,data,size,readlevel);
    if (rv!=RC_ERROR_OPEN)
    {
        return rv;
    }

   rv = paragen_get( &mon_obj, index,data,size,readlevel);
   if (rv!=RC_ERROR_OPEN)
   {
       return rv;
   }


   /* process other requests here */



    return rv;

}

static RC parameter_set(  U32 index, void const * data, U16 size, U32  writelevel )
{
   RC rv=RC_ERROR_OPEN;

   rv = paragen_set( &calibration_obj, index,data,size,writelevel);
   if (rv!=RC_ERROR_OPEN)
   {
      return rv;
   }

   rv = paragen_set( &service_obj, index,data,size,writelevel);
   if (rv!=RC_ERROR_OPEN)
   {
      return rv;
   }

   rv = paragen_set( &production_obj, index,data,size,writelevel);
   if (rv!=RC_ERROR_OPEN)
   {
      return rv;
   }

   return rv;
   /* process other requests here */

}

/*===============================================[ private functions ]================================================*/


/***********************************************************************************************************************
*                             Copyright 2020 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
